package com.yugao.fintech.antelope.infra.weixin.mp.handler.wx.user;

import com.yugao.fintech.antelope.infra.weixin.mp.MpContextHolder;
import com.yugao.fintech.antelope.infra.weixin.mp.service.MpUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 取消关注的事件处理器
 *

 */
@Component
@Slf4j
public class UnsubscribeHandler implements WxMpMessageHandler {

    @Autowired
    @Lazy // 延迟加载，解决循环依赖的问题
    private MpUserService mpUserService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        log.info("[handle][粉丝({}) 取消关注]", wxMessage.getFromUser());
        mpUserService.updateUserUnsubscribe(MpContextHolder.getParam().getAppId(), wxMessage.getFromUser());
        return null;
    }

}
